home *** CD-ROM | disk | FTP | other *** search
/ Aminet 32 / Aminet 32 (1999)(Schatztruhe)[!][Aug 1999].iso / Aminet / dev / lang / Python151_Src.lha / Python1.5_Source / Amiga / chown.c < prev    next >
C/C++ Source or Header  |  1996-06-11  |  5KB  |  200 lines

  1. RCS_ID_C="$Id: chown.c,v 4.2 1994/09/29 23:09:02 jraja Exp $";
  2. /*
  3.  *      chown.c - chown() for usergroup link library
  4.  *
  5.  *      Copyright © 1994 AmiTCP/IP Group, 
  6.  *                       Network Solutions Development Inc.
  7.  *                       All rights reserved.
  8.  * 
  9.  *      This function is based on SetOwner37 code
  10.  *      Copyright © 1993 by Geert Uytterhoeven
  11.  */
  12.  
  13.  
  14. /****** net.lib/chown *********************************************************
  15.  
  16.     NAME
  17.         chown - change owner and group of a file
  18.  
  19.     SYNOPSIS
  20.         #include <unistd.h>
  21.  
  22.         success = chown(path, owner, group)
  23.  
  24.         int chown(const char *, uid_t, gid_t);
  25.  
  26.     DESCRIPTION
  27.         The owner ID and group ID of the file named by path or referenced by
  28.         fd is changed as specified by the arguments owner and group.  The
  29.         owner of a file may change the group to a group of which he or she is
  30.         a member, but the change owner capability is restricted to the
  31.         super-user.
  32.  
  33.         Chown() clears the set-user-id and set-group-id bits on the file to
  34.         prevent accidental or mischievious creation of set-user-id and
  35.         set-group-id programs.
  36.  
  37.         One of the owner or group id's may be left unchanged by specifying it
  38.         as -1.
  39.  
  40.         If the final component of path is a symbolic link, the ownership and
  41.         group of the symbolic link is changed, not the ownership and group of
  42.         the file or directory to which it points.
  43.  
  44.     RETURN VALUES
  45.         Zero is returned if the operation was successful; -1 is returned if an
  46.         error occurs, with a more specific error code being placed in the
  47.         global variable errno.
  48.  
  49.     ERRORS
  50.         Chown() will fail and the file will be unchanged if:
  51.  
  52.         [ENOTDIR]     A component of the path prefix is not a directory.
  53.  
  54.         [EINVAL]      The pathname contains a character with the high-order
  55.                       bit set.
  56.  
  57.         [ENAMETOOLONG]
  58.                       A component of a pathname exceeded 80 characters, or an
  59.                       entire path name exceeded 1023 characters.
  60.  
  61.         [ENOENT]      The named file does not exist.
  62.  
  63.         [EACCES]      Search permission is denied for a component of the path
  64.                       prefix.
  65.  
  66.         [ELOOP]       Too many symbolic links were encountered in translating
  67.                       the pathname.
  68.  
  69.         [EPERM]       The effective user ID is not the super-user.
  70.  
  71.         [EROFS]       The named file resides on a read-only file system.
  72.  
  73.         [EFAULT]      Path points outside the process's allocated address
  74.                       space.
  75.  
  76.         [EIO]         An I/O error occurred while reading from or writing to
  77.                       the file system.
  78.  
  79.     SEE ALSO
  80.         chmod(2)
  81.  
  82. *****************************************************************************
  83. */
  84.  
  85. #include <libraries/usergroup.h>
  86. #include <proto/dos.h>
  87. #include <dos/dosextens.h>
  88.  
  89. #include "netlib.h"
  90.  
  91. #ifndef ACTION_SET_OWNER    
  92. #define ACTION_SET_OWNER    1036
  93. #endif
  94.  
  95. int chown(const char *name, uid_t uid, gid_t gid)
  96. {
  97.   BPTR lock;
  98.   short rc = 0;        /** WAS -1!! Fixed 10/6/96 I.J. **/
  99.  
  100.   if (lock = Lock((STRPTR)name, ACCESS_READ)) {
  101.     if (uid == -1 || gid == -1) {
  102.       /* XXX We are supposed to do stat() and find out the suitable value */
  103.       
  104.     }
  105.     rc = DoPkt(((struct FileLock *)BADDR(lock))->fl_Task,
  106.            ACTION_SET_OWNER, 
  107.            NULL, lock, MKBADDR("\0"), 
  108.            (UG2MU(uid) << 16) | UG2MU(gid), NULL);
  109.     UnLock(lock);
  110.   }
  111.  
  112.   if (!rc) {
  113.     set_errno(IoErr());
  114.     return -1;
  115.   } else {
  116.     return 0;
  117.   }
  118. }
  119.  
  120. #ifdef DEBUGGING
  121. #include <proto/usergroup.h>
  122. #include <stdlib.h>
  123. #include <string.h>
  124. #include <exec/execbase.h>
  125. int errno;
  126. extern struct ExecBase *SysBase;
  127.  
  128. void PrintUserFault(LONG code, const UBYTE *banner);
  129.  
  130. const static char usage[] = "usage: chown [-fR] owner[:group] file ...";
  131.  
  132. void main(int argc, char *argv[])
  133. {
  134.   struct Process *p = (struct Process *)SysBase->ThisTask;
  135.   BPTR Stderr = p->pr_CES ? p->pr_CES : p->pr_COS;
  136.  
  137.   short perrors = 1, recursive = 0;
  138.   uid_t uid; gid_t gid = -1;
  139.   char *group, *user;
  140.  
  141.   while (argc > 1 && argv[1][0] == '-') {
  142.     switch (argv[1][1]) {
  143.     case 'f':
  144.       perrors = 0;
  145.       break;
  146.     case 'R':
  147.       recursive = 1;
  148.       break;
  149.     default:
  150.       FPrintf(Stderr, usage);
  151.       exit(10);
  152.     }
  153.     argv++, argc--;
  154.   }
  155.  
  156.   if (argc <= 2) {
  157.     FPrintf(Stderr, usage);
  158.     exit(10);
  159.   }
  160.  
  161.   user = argv[1]; argv++, argc--;
  162.   group = rindex(user, ':');
  163.  
  164.   if (group) {
  165.     *group++ = '\0';
  166.     if (StrToLong(group, &gid) < 1) {
  167.       struct group *gr = getgrnam(group);
  168.       if (gr) {
  169.     gid = gr->gr_gid;
  170.       } else {
  171.     if (perrors)
  172.       PrintUserFault(errno, "chown: getgrnam");
  173.     exit(RETURN_ERROR);
  174.       }
  175.     }
  176.   }
  177.  
  178.   if (StrToLong(user, &uid) < 1) {
  179.     struct passwd *pw = getpwnam(user);
  180.     if (pw) {
  181.       uid = pw->pw_uid;
  182.     } else {
  183.       if (perrors)
  184.     PrintUserFault(errno, "chown: getpwnam");
  185.       exit(RETURN_ERROR);
  186.     }
  187.   }
  188.  
  189.   while (argc-- > 1) {
  190.     if (chown(argv++[1], uid, gid) == -1) {
  191.       if (perrors)
  192.     PrintUserFault(errno, "chmod");
  193.       exit(RETURN_ERROR);
  194.     }
  195.   }
  196.  
  197.   exit(0);
  198. }
  199. #endif
  200.